//
//  TopTabView.m
//  iBaby365v2
//
//  Created by Kirinrin on 14-7-4.
//  Copyright (c) 2014年 suplus. All rights reserved.
//

#import "TopTabView.h"

static const NSInteger TagOffset = 1000;

@implementation TopTabView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}



// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    NSUInteger index = 0;
    for (NSString *title in self.tabTitles) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.tag = index;
        
        button.titleLabel.font = [UIFont boldSystemFontOfSize:18];
        button.titleLabel.shadowOffset = CGSizeMake(0, 1.0);
        [button setTitle:title forState:UIControlStateNormal];
        
        [button addTarget:self action:@selector(tabButtonPressed:) forControlEvents:UIControlEventTouchDown];
        [self deselectTabButton:button];
		[self addSubview:button];
        
		++index;
    }
}

- (void)deselectTabButton:(UIButton *)button
{
	[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
	UIImage *image = [[UIImage imageNamed:@"MHTabBarInactiveTab"] stretchableImageWithLeftCapWidth:1 topCapHeight:0];
	[button setBackgroundImage:image forState:UIControlStateNormal];
	[button setBackgroundImage:image forState:UIControlStateHighlighted];
    
	[button setTitleColor:[UIColor colorWithRed:175/255.0f green:85/255.0f blue:58/255.0f alpha:1.0f] forState:UIControlStateNormal];
	[button setTitleShadowColor:[UIColor whiteColor] forState:UIControlStateNormal];
}

- (void)tabButtonPressed:(UIButton *)sender
{
	[self setSelectedIndex:sender.tag - TagOffset animated:YES];
    
}

- (void)setSelectedIndex:(NSUInteger)newSelectedIndex
{
	[self setSelectedIndex:newSelectedIndex animated:NO];
}

- (void)setSelectedIndex:(NSUInteger)newSelectedIndex animated:(BOOL)animated
{
//	NSAssert(newSelectedIndex < [self.viewControllers count], @"View controller index out of bounds");
    
	if ([self.delegate respondsToSelector:@selector(TopTabView:shouldSelectAtIndex:)])
	{
		if (![self.delegate TopTabView:self shouldSelectAtIndex:newSelectedIndex])
			return;
	}
    NSLog(@"is hidden %x", self.isHidden);
	
}

@end
